package com.smec.remes.module.request;

import android.content.Context;
import android.util.Log;

import com.smec.remes.base.SmecBasePresenter;
import com.smec.remes.bean.AbleMatnr;
import com.smec.remes.bean.GetUserName;
import com.smec.remes.bean.MantrCheck;
import com.smec.remes.bean.MantrStatus;
import com.smec.remes.bean.SubmitApply;
import com.smec.remes.bean.SubmitDetails;
import com.smec.remes.smecManager.SmecRetrofit.BaseSubscriber;
import com.smec.remes.smecManager.SmecRetrofit.HttpResult;

import java.util.ArrayList;
import java.util.HashMap;


import rx.Observable;

/**
 * Created by Administrator on 2017/11/23.
 */

public class SmecRequestPresenter extends SmecBasePresenter {

    public static final String GET_ALL_PRICE_SUCCESS="GET_ALL_PRICE_SUCCESS";
    public static final String GET_ALL_PRICE_FAIL="GET_ALL_PRICE_FAIL";


    public SmecRequestPresenter(Context mContext) {
        super(mContext);
    }

    public void getUsername(String condition,String orgType,int pageNumber) {
        HashMap<String,Object> map=new HashMap<>();
        map.put("condition",condition);
        map.put("orgType",orgType);
        map.put("pageSize",10);
        map.put("pageNumber",pageNumber);
        Observable<HttpResult<ArrayList<GetUserName>>> observable=smecPackingService.getUserName(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<GetUserName>>>() {
            @Override
            public void onNext(HttpResult<ArrayList<GetUserName>> arrayListHttpResult) {
                mRxbus.post("USERNAME",arrayListHttpResult.getData());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post("USERNAMEFAIL","");
            }
        });
    }

    public void _getUsername(String condition,String orgType,int pageNumber) {
        HashMap<String,Object> map=new HashMap<>();
        map.put("condition",condition);
        map.put("orgType",orgType);
        map.put("pageSize",10);
        map.put("pageNumber",pageNumber);
        Observable<HttpResult<ArrayList<GetUserName>>> observable=smecPackingService.getUserName(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<GetUserName>>>() {
            @Override
            public void onNext(HttpResult<ArrayList<GetUserName>> arrayListHttpResult) {
                mRxbus.post("_USERNAME",arrayListHttpResult.getData());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post("_USERNAMEFAIL","");
            }
        });
    }

    public void submitApply(final SubmitApply submitApply ){
        Observable<HttpResult> observable=smecPackingService.submitApply(submitApply);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult>() {
            @Override
            public void onNext(HttpResult httpResult) {
                if(submitApply.getType().equals("SUBMIT")){
                    mRxbus.post("SUBMIT",httpResult.getCode());
                }else{
                    mRxbus.post("SAVE",httpResult.getCode());
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                Log.w("submitApply","FAIL");
            }
        });
    }

    public void ableMatnr(String condition, int pageNumber){
        HashMap<String,Object> map=new HashMap<>();
        map.put("condition",condition);
        map.put("type","APPLY");
        map.put("pageSize",10);
        map.put("pageNumber",pageNumber);

        Observable<HttpResult<ArrayList<AbleMatnr>>> observable=smecPackingService.ableMatnr(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<AbleMatnr>>>() {
            @Override
            public void onNext(HttpResult<ArrayList<AbleMatnr>> arrayListHttpResult) {
                ArrayList<AbleMatnr> arrayList=new ArrayList<>();
                arrayList=arrayListHttpResult.getData();
                mRxbus.post("SEARCH",arrayList);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post("SEARCHFAIL","");
            }
        });
    }

    /**
     * 维保站APP-物料校验
     */
    public void checkMantr(MantrCheck mantrCheck){
        Observable<HttpResult<ArrayList<MantrStatus>>> observable=smecPackingService._mantrCheck(mantrCheck);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<MantrStatus>>>() {
            @Override
            public void onNext(HttpResult<ArrayList<MantrStatus>> arrayListHttpResult) {
                mRxbus.post("CHECK",arrayListHttpResult);
            }
        });
    }

    public void _submitDetails(String applyOrderNo) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("applyOrderNo", applyOrderNo);
        Observable<HttpResult<ArrayList<SubmitDetails>>> observable = smecPackingService.submitDetails(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<SubmitDetails>>>() {
            @Override
            public void onNext(HttpResult<ArrayList<SubmitDetails>> arrayListHttpResult) {
                ArrayList<SubmitDetails> arrayList=arrayListHttpResult.getData();
                mRxbus.post("DETAILS",arrayList);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post("DETAILSFAIL","FAIL");
            }
        });
    }

    /**
     * 获取所有物料的价格
     * @param list
     */
    public void getAllPrice(ArrayList<AbleMatnr> list){
        HashMap<String, Object> map = new HashMap<>();
        map.put("matnrList", list);
        Observable<HttpResult<SmecMatnrMsg>> observable=smecPackingService.getAllMaPrice(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<SmecMatnrMsg>>() {
            @Override
            public void onNext(HttpResult<SmecMatnrMsg> arrayListHttpResult) {
                mRxbus.post(GET_ALL_PRICE_SUCCESS,arrayListHttpResult.getData());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(GET_ALL_PRICE_FAIL,new Object());
            }
        });
    }

    public void tenMantr(){
        Observable<HttpResult<ArrayList<AbleMatnr>>> observable=smecPackingService._tenMantr(new Object());
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<AbleMatnr>>>() {
            @Override
            public void onNext(HttpResult<ArrayList<AbleMatnr>> arrayListHttpResult) {
                ArrayList<AbleMatnr> arrayList=new ArrayList<>();
                arrayList=arrayListHttpResult.getData();
                mRxbus.post("ten",arrayList);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post("tenFail","");
            }
        });
    }
}
